<?php

/**
 * Description of SiteController
 *
 * @author Eureka
 */
//Yii::import('application.vendors.*');
//require_once('facebook/facebook.php');

class SiteController extends CController
{

    public $menus = array();

    public function init()
    {
        $this->layout = 'layout';

//		$social = Social::getInstance();
//		$user = $social->getUser();
//		if (empty($user))
//		{
//			$social->authorize();
//		}
    }

    public function actionGrid()
    {
        $criteria = new CDbCriteria();

        $item_count = Items::model()->count($criteria);
        $page_size = 18;
        $pages = new CPagination($item_count);
        $pages->setPageSize($page_size);
        $pages->applyLimit($criteria);

        $items = Items::model()->with('shops', 'item_tags')->findAll($criteria);
        $model = new Items;
        $this->render('grid', array(
            'items' => $items,
            'pages' => $pages,
            'dataProvider' => $model->listItem()
        ));
    }
    
    //put your code here
    public function actionIndex()
    {
        $model = new SearchForms();

        $criteria = new CDbCriteria();

        $item_count = Items::model()->count($criteria);
        $page_size = 18;

        $pages = new CPagination($item_count);
        $pages->setPageSize($page_size);
        $pages->applyLimit($criteria);

        $items = Items::model()->with('shops', 'item_tags')->findAll($criteria);

        $tag_types = TagTypes::model()->findAll();

        $this->render('index', array(
            'items' => $items,
            'pages' => $pages,
            'model' => $model
        ));
    }

    public function actionItems()
    {
        $tag = Yii::app()->getRequest()->getParam('tag');

        $criteria = new CDbCriteria();

        if (!empty($tag))
        {
            $criteria->alias = 'i';
            $criteria->select = 'i.*, t.name as tag_name';
            $criteria->join = 'LEFT JOIN item_tags it ON (it.item_id = i.id)';
            $criteria->join .= 'LEFT JOIN tags t ON (t.id = it.tag_id)';
            $criteria->addCondition('it.tag_id = :tag_id');
            $criteria->params = array(':tag_id' => $tag);
        }

        $item_count = Items::model()->count($criteria);
        $page_size = 12;

        $pages = new CPagination($item_count);
        $pages->setPageSize($page_size);
        $pages->applyLimit($criteria);

        $items = Items::model()->with('shops', 'item_tags')->findAll($criteria);

        $tag_types = TagTypes::model()->findAll();

        $this->render('items', array(
            'items' => $items,
            'pages' => $pages,
            'tag_id' => $tag,
            'tag_types' => $tag_types
        ));
    }

    public function actionShops()
    {
        $location = Yii::app()->getRequest()->getParam('location');

        $criteria = new CDbCriteria();

        if (!empty($location))
        {
            $criteria->alias = 's';
            $criteria->select = 's.*';
            $criteria->join = 'JOIN shop_locations sl ON (sl.shop_id = s.id)';
            $criteria->addCondition('sl.location_id = :location_id');
            $criteria->params = array(':location_id' => $location);
        }

        $shop_count = Shops::model()->count($criteria);

        $page_size = 2;

        $pages = new CPagination($shop_count);
        $pages->setPageSize($page_size);
        $pages->applyLimit($criteria);

        $shops = Shops::model()->findAll($criteria);

        $locations = Location::model()->findUsedLocation();

        $this->render('shops', array(
            'shops' => $shops,
            'pages' => $pages,
            'locations' => $locations
        ));
    }

    public function actionCreate()
    {
        $model = new ArticlesForm();
        if (isset($_POST['ArticlesForm']))
        {
            $model->attributes = $_POST['ArticlesForm'];
            if ($model->validate())
            {
                echo $model->title;
                echo $model->body;
                Yii::app()->user->setFlash('create', 'Thank you for contacting us. We will respond to you as soon as possible.');
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionShop_Details()
    {
        $shop_id = Yii::app()->getRequest()->getParam('id');
        $shop = Shops::model()->findByPk($shop_id);

        $shop_contacts = ShopContacts::model()->findByShopId($shop_id);

        $criteria = new CDbCriteria();
        $criteria->addCondition('shop_id= :shop_id');
        $criteria->params = array(':shop_id' => $shop_id);

        $item_count = Collections::model()->count($criteria);
        $page_size = 2;

        $pages = new CPagination($item_count);
        $pages->setPageSize($page_size);
        $pages->applyLimit($criteria);

        $collections = Collections::model()->with('shops')->findAll($criteria);

        $this->menus = array('label' => $shop->name, 'url' => array('site/shop_details'));

        $this->render('shop_details', array(
            'shop' => $shop,
            'shop_contacts' => $shop_contacts,
            'collections' => $collections,
            'pages' => $pages
        ));
    }

    public function actionCollection()
    {
        $collection_id = Yii::app()->getRequest()->getParam('id');

        $criteria = new CDbCriteria();
        $criteria->addCondition('collection_id=:collection_id');
        $criteria->params = array(':collection_id' => $collection_id);

        $item_count = CollectionItems::model()->count($criteria);
        $page_size = 2;

        $pages = new CPagination($item_count);
        $pages->setPageSize($page_size);
        $pages->applyLimit($criteria);

        $collections = CollectionItems::model()->with('items')->findAll($criteria);

        $this->menus = array('label' => 'Bộ sưu tập', 'url' => array('site/collection'));

        $this->render('collection', array(
            'collections' => $collections,
            'pages' => $pages,
        ));
    }

    public function actionSearch()
    {
        $this->menus = array('label' => 'Kết quả tìm kiếm', 'url' => array('site/search'));

        $criteria = new CDbCriteria();

        $model = new SearchForms();
        $mode = 1;

        if (!empty($_POST['SearchForms']))
        {
            $model->setAttributes($_POST['SearchForms']);
            if ($model->validate())
            {
                $keyword = $_POST['SearchForms']['keyword'];
                $mode = $_POST['SearchForms']['mode'];
            }
        }

        if ($mode == 1)
        {
            if (!empty($keyword))
            {
                $criteria->alias = 'i';
                $criteria->select = 'i.*';
                $expr = mysql_escape_string($keyword);
                $criteria->addCondition("MATCH (i.title,i.description) AGAINST ('{$expr}' IN BOOLEAN MODE)");
            }

            $item_count = Items::model()->count($criteria);
            $page_size = 2;

            $pages = new CPagination($item_count);
            $pages->setPageSize($page_size);
            $pages->applyLimit($criteria);

            $items = Items::model()->findAll($criteria);


            $this->render('search', array(
                'items' => $items,
                'pages' => $pages,
                'mode' => $mode,
                'model' => $model
            ));
        }
        else
        {
            if (!empty($keyword))
            {
                $criteria->alias = 's';
                $criteria->select = 's.*';
                $expr = mysql_escape_string($keyword);
                $criteria->addCondition("MATCH (s.name,s.address) AGAINST ('{$expr}' IN BOOLEAN MODE)");
            }

            $shop_count = Shops::model()->count($criteria);

            $page_size = 2;

            $pages = new CPagination($shop_count);
            $pages->setPageSize($page_size);
            $pages->applyLimit($criteria);

            $shops = Shops::model()->findAll($criteria);


            $this->render('search', array(
                'shops' => $shops,
                'pages' => $pages,
                'mode' => $mode,
                'model' => $model
            ));
        }
    }

    public function actionRedirect()
    {
        //Util::topRedirect(Config::params('fanpageUrl')) . '/app_' . Config::params('appId');
        $this->redirect('/site/index');
    }

}

?>
